/*
 * Copyright (c) 2023, 2023,yzc.cn All rights reserved.
 *
 */
package cn.yzc;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * <p>Project: SpringPro - Log</p>
 * <p>Powered by yzc On 2023-10-20 16:28:38</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
public class Log {
    /**
     * 前置增强逻辑
     *
     * @param joinPoint 可以获取执行的目标方法的相关信息（类 方法 参数...）
     */
    public void before(JoinPoint joinPoint) {
        System.out.println("------------before--------------");
        System.out.println("全限定名:" + joinPoint.getTarget().getClass().getName());
        System.out.println("方法名:" + joinPoint.getSignature().getName());
        System.out.println("开始执行");
    }

    public void after(JoinPoint joinPoint) {
        System.out.println("全限定名:" + joinPoint.getTarget().getClass().getName());
        System.out.println("方法名:" + joinPoint.getSignature().getName());
        System.out.println("执行结束");
        System.out.println("------------after--------------");
    }

    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //方法执行前
        long begin = System.currentTimeMillis();
        System.out.println(begin);
        //获取执行方法返回值
        Object obj = joinPoint.proceed();
        //方法执行后
        System.out.println(joinPoint.getTarget().getClass().getName() + joinPoint.getSignature().getName() + "所用时间" + (System.currentTimeMillis() - begin));
        return obj;
    }

    public void throwing(JoinPoint joinPoint, Exception e) {
        System.out.println(joinPoint.getTarget().getClass() + "方法名" + joinPoint.getSignature().getName());
        System.out.println("异常信息" + e.getMessage());
    }

    public void returning(JoinPoint joinPoint, Object args) {
        System.out.println("放回值为：" + args);
    }
}
